package com.wcy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcy.annotation.aop.PreAuth;
import com.wcy.entity.SrMenu;
import com.wcy.entity.queryVo.QueryConvertor;
import com.wcy.service.ISrMenuService;
import com.wcy.util.JwtTokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/srMenu")
public class SrMenuController {

    @Autowired
    private ISrMenuService srMenuService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @PreAuth("MENU:QUERY")
    @GetMapping("/page")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Page<SrMenu>> getPage(@RequestParam(defaultValue = "1") int pageNum,
                                                @RequestParam(defaultValue = "10") int pageSize,
                                                HttpServletRequest request) {
        String queryData = request.getHeader("X-Query-Data");
        System.out.println(queryData);
        QueryWrapper wrapper = new QueryConvertor(queryData).convert(true);
        Page<SrMenu> page = srMenuService.page(new Page<>(pageNum, pageSize),wrapper);
        return ResponseEntity.ok(page);
    }

    @PreAuth("MENU:QUERY")
    @GetMapping("/list")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<List<SrMenu>> getList(HttpServletRequest request) {
        String queryData = request.getHeader("X-Query-Data");
        System.out.println(queryData);
        QueryWrapper wrapper = new QueryConvertor(queryData).convert(true);
        List<SrMenu> list = srMenuService.list(wrapper);
        return ResponseEntity.ok(list);
    }

    @PreAuth("MENU:ADD")
    @PostMapping("/add")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Void> add(@RequestBody SrMenu srMenu) {
        srMenuService.save(srMenu);
        return ResponseEntity.ok().build();
    }

    @PreAuth("MENU:ALTER")
    @PutMapping("/update")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Void> update(@RequestBody SrMenu srMenu) {
        if(Objects.equals(srMenu.getId(), srMenu.getPid()))
            return ResponseEntity.badRequest().build();
        srMenuService.updateById(srMenu);
        return ResponseEntity.ok().build();
    }

    @PreAuth("MENU:DELETE")
    @DeleteMapping("/delete/{id}")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Void> delete(@PathVariable Integer id) {
        srMenuService.removeById(id);
        return ResponseEntity.ok().build();
    }

    @PreAuth("AUTHENTICATION")
    @GetMapping("authenticated/list")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<List<SrMenu>> getUserMenuList(HttpServletRequest request) {
        List<SrMenu> list = srMenuService.getUserMenuList(request);
        return ResponseEntity.ok(list);
    }
}